<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 07:38 AM
 */
class PHZ_Mopai_Peng
{
    public static function __peng( $room_id , $who_uid , $card_number , $db_connect ){
        //取出房间信息
        $room_info    = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //当前用户是否有碰牌的权限
        $user_index   = PHZ_Tools::__get_user_in_room_index( $room_info , $who_uid );
        if( $room_info['p'. $user_index .'_is_peng'] != 1 ){
            return false;
        }
        //取出该房间对应的手牌相关的card信息 - 应该是4条记录
        $card_info    = $db_connect->select('*')->from('tt_room_card')->where("jid = '". $room_info['room_js'] ."' and rid= '". $room_id ."' ")->query();
        //设置当前房间摸牌人以及牌号 - 被碰掉了，清空
        PHZ_Room::__setThisMopCard( $room_info['room_id'] , 0 , 0 , true , $db_connect );
        //获取我的手牌信息跟谁打的这张牌的信息
        foreach( $card_info as $key => $value ){
            if( $value['uid'] == $who_uid ){$my_card_info  = $value;}
        }
        //删除碰牌者手牌中2张牌,添加3张xp - 并同时添加一个碰的牌 - card_peng - 用来记录该用户碰过的牌
        $my_sp        = explode(",",$my_card_info['sp']);
        foreach( $my_sp as $key => $value ){//删除手中2个碰的牌，碰肯定是2个，都删了就行了；
            if( $value == $card_number ){
                unset( $my_sp[$key] );
            }
        }
        $my_sp        = implode(",", $my_sp );
        $my_xp        = $my_card_info['xp'] . $card_number  . "_" .$card_number  . "_" .$card_number  . ",";//加入3张下牌
        $card_peng    = $my_card_info['card_peng'] . $card_number . ",";
        $db_connect->update('tt_room_card')->cols(array('sp'=> $my_sp , 'xp' => $my_xp , 'card_peng' => $card_peng ))->where('id=' . $my_card_info['id'] )->query();
        PHZ_Tools::__set_room_alluser_hu_chi_peng_0( $room_id , $db_connect );//属性归0
        //增加息数
        PHZ_SuanXi::__xi( $room_info , $who_uid , "peng" , $card_number , $db_connect );
        //用户是否有进字免打的权限？如果有；则直接下家摸牌操作
        $mianda = false;
        if( PHZ_Card::__get_User_TIPAO_Free_to_Play( $room_info , $who_uid , $db_connect ) == true ){
            $mianda = true;
        }
        //向其所有人发送牌被碰的消息，且转带出牌权；这个时候摸牌权还是0（假如这个时候有人可以吃，那么吃的按钮是要被清楚掉的）
        $temp = array();
        $temp['pl']          = 5;//5为用户碰牌
        $temp['card_number'] = $card_number;//牌号
        $temp['put_uid']     = $mianda == true ? 0 : $who_uid;
        $temp['mop_uid']     = 0;//全部人丧失摸牌权
        PHZ_Log::__send_user_log( $room_info , $who_uid , $temp , $db_connect );
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
        //如果免打，则吃碰畏牌的人下家自动摸牌(2017.11.21)
        if( $mianda == true ){
            PM_n::__index( $room_info , $who_uid , 0 , $db_connect );
        }
        //执行结束
        return true;
    }
}